package com.wanhong.springboot.ssm.framework.mq;


import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author w_yu
 */
@Component
public class KafkaProducer {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    private static final Logger log = LoggerFactory.getLogger(KafkaProducer.class);

    /**
     * 发送用户消息
     * @param obj 信息
     */
    public void sendUserMessage(Object obj) {

        GsonBuilder builder = new GsonBuilder();
        builder.setPrettyPrinting();
        builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String message = builder.create().toJson(obj);
        kafkaTemplate.send("topic-user", message);
        log.info("\n生产消息至Kafka\n" + message);
    }

}
